using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace BCW.Friends
{
	public class FriendsUIGame : MonoBehaviour
	{
		public static FriendsUIGame instance;

		private int maxInvitePlayers = 3;

		public int curInvitePlayer;

		private int maxInviteToBattlePlayers = 3;

		public int curInviteToBattlePlayer;

		private float timeForCleanMaxInvite = 300f;

		public GameObject msgError;

		public List<string> listSendedIds = new List<string>();

		private butScrollPlayers gameLeaderboard;

		private PlayerInfoPanel gameTDMLeaderboard;

		private IEnumerator timer;

		public static event Action CantSendInvite;

		private void Awake()
		{
			CleanInvitePlayer();
			instance = this;
		}

		private void OnDestroy()
		{
			instance = null;
		}

		public void AddFriend(string idPlayers, Action<ServerResponse> callback)
		{
			if (curInvitePlayer > 0)
			{
				FriendsController.instance.InviteFriend(idPlayers, callback);
			}
		}

		public void CleanInvitePlayer()
		{
			curInvitePlayer = maxInvitePlayers;
			curInviteToBattlePlayer = maxInviteToBattlePlayers;
		}

		public bool CanSendInvite()
		{
			return curInvitePlayer > 0;
		}

		public bool CanSendInviteToBattle()
		{
			return curInviteToBattlePlayer > 0;
		}

		private IEnumerator TimerForFreeGame()
		{
			yield return new WaitForSeconds(timeForCleanMaxInvite);
			CleanInvitePlayer();
			timer = null;
		}

		public void OKButon()
		{
			if (gameLeaderboard != null)
			{
				AddFriend(gameLeaderboard.idPlayer, gameLeaderboard.CallBackFriend);
				gameLeaderboard.ChangeTexture(FriendsButton.InactiveFriend);
			}
			if (gameTDMLeaderboard != null)
			{
				AddFriend(gameTDMLeaderboard.idPlayer, gameTDMLeaderboard.CallBackFriend);
				gameTDMLeaderboard.ChangeTexture(FriendsButton.InactiveFriend);
			}
			curInvitePlayer--;
			if (FriendsUIGame.CantSendInvite != null && curInvitePlayer <= 0)
			{
				FriendsUIGame.CantSendInvite();
			}
			if (curInvitePlayer <= 0 && GameController.instance.curTypeGame == TypeGame.normal && !settings.offlineMode && timer == null)
			{
				timer = TimerForFreeGame();
				StartCoroutine(timer);
			}
			gameLeaderboard = null;
			msgError.SetActive(false);
		}

		public void CancelButton()
		{
			gameLeaderboard = null;
			msgError.SetActive(false);
		}

		public void ShowWarningMsg(butScrollPlayers link)
		{
			gameLeaderboard = link;
			msgError.SetActive(true);
		}

		public void ShowWarningMsg(PlayerInfoPanel link)
		{
			gameTDMLeaderboard = link;
			msgError.SetActive(true);
		}
	}
}
